﻿using AutoMapper;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using ShanNiang.Hosting;
using ShanNiang.Hosting.Common;
using ShanNiang.Hosting.Enumerate;
using ShanNiang.Service.Interfaces;
using ShanNiang.Service.PackingModels.Requests;
using ShanNiang.Service.PackingModels.Responses;
using ShanNiang.Service.PackingModes.Responses;

namespace ShanNiang.Service.Implementations
{
    /// <summary>
    /// 雪克码服务
    /// </summary>
    public class XkmService : LocalService, IXkmService
    {
        private readonly PackingConfig _packingConfig;
        private readonly UserContext _userContext;
        private readonly IMapper _mapper;
        public XkmService(PackingConfig packingConfig, UserContext userContext,IMapper mapper, ILogger<LocalService> logger) : base(logger)
        {
            _packingConfig = packingConfig;
            _userContext = userContext;
            _mapper= mapper;
            //_clientService = clientService;
        }
        /// <summary>
        /// 获取箱码
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<BoxCodeResponse?> GetBoxCodeAsync(GetBoxCodeRequest request)
        {
            var client = GetClient<GetBoxCodeRequest>();
            var response = await client.GetResponse<ApiResult<BoxCodeResponse>>(request);
            return GetInnerResponse(response);
        }
        /// <summary>
        /// 获取分页商品
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<ItemResponse?> GetItemAsync(GetItemRequest request)
        {
            var client = GetClient<GetItemRequest>();
            var response = await client.GetResponse<ApiResult<ItemResponse>>(request);
            return GetInnerResponse(response);
        }
        /// <summary>
        /// 分页获取商品
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<PageResponse<ItemResponse>?> GetItemsPageAsync(GetItemsPageRequest request)
        {
            var client = GetClient<GetItemsPageRequest>();
            var response = await client.GetResponse<ApiResult<PageResponse<ItemResponse>>>(request);
            return GetInnerResponse(response);
        }

        /// <summary>
        /// 获取分页箱码
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<PageResponse<BoxCodeResponse>?> GetPackingBoxesPageAsync(GetPackingBoxesPageRequest request) 
        {
            var client = GetClient<GetPackingBoxesPageRequest>();
            var response = await client.GetResponse<ApiResult<PageResponse<BoxCodeResponse>>>(request);
            return GetInnerResponse(response);
        }

        /// <summary>
        /// 获取装箱记录里的商品
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<List<ItemResponse>?> GetPackingItemsAsync(GetPackingItemsRequest request) 
        {
            var client = GetClient<GetPackingItemsRequest>();
            var response = await client.GetResponse<ApiResult<List<ItemResponse>>>(request);
            return GetInnerResponse(response);
        }
        /// <summary>
        /// 获取装箱记录
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<PageResponse<PackingRecordResponse>?> GetPackingRecordsPageAsync(GetPackingRecordsPageRequest request) 
        {
            var client = GetClient<GetPackingRecordsPageRequest>();
            var response = await client.GetResponse<ApiResult<PageResponse<PackingRecordResponse>>>(request);
            return GetInnerResponse(response);
        }
        /// <summary>
        /// 获取装箱记录里的装箱人
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<List<PackingUserResponse>?> GetPackingUsersRequestAsync(GetPackingUsersRequest request)
        {
            var client = GetClient<GetPackingUsersRequest>();
            var response = await client.GetResponse<ApiResult<List<PackingUserResponse>>>(request);
            return GetInnerResponse(response);
        }
        /// <summary>
        /// 获取托盘数据
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<PalletResponse?> GetPalletAsync(GetPalletRequest request) 
        {
            var client = GetClient<GetPalletRequest>();
            var response = await client.GetResponse<ApiResult<PalletResponse>>(request);
            return GetInnerResponse(response);
        }
        /// <summary>
        /// 获取分页已装的托
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<PageResponse<PalletDetailResponse>?> GetPalletsPageAsync(GetPalletsPageRequest request)
        {
            var client = GetClient<GetPalletsPageRequest>();
            var response = await client.GetResponse<ApiResult<PageResponse<PalletDetailResponse>>>(request);
            return GetInnerResponse(response);
        }
        /// <summary>
        /// 获取托盘详细数据
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<PalletDetailResponse?> GetPalletDetailAsync(GetPalletDetailRequest request) 
        {
            var client = GetClient<GetPalletDetailRequest>();
            var response = await client.GetResponse<ApiResult<PalletDetailResponse>>(request);
            return GetInnerResponse(response);
        }

        /// <summary>
        /// 获取用户摘要
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<UserWorkSummaryResponse?> GetUserWorkSummaryAsync(GetUserWorkSummaryRequest request)
        {
            var client = GetClient<GetUserWorkSummaryRequest>();
            var response = await client.GetResponse<ApiResult<UserWorkSummaryResponse>>(request);
            return GetInnerResponse(response);
        }
        /// <summary>
        /// 登陆或注册
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<UserResponse?> LogionOrRegistAsync(LoginOrRegistRequest request)
        {
            var client =  GetClient<LoginOrRegistRequest>();
            var response = await client.GetResponse<ApiResult<UserResponse>>(request);
            return GetInnerResponse(response);
        }
        /// <summary>
        /// 封箱
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<int> PackUpBoxAsync(PackUpBoxRequest request)
        {
            var client = GetClient<PackUpBoxRequest>();
            var response = await client.GetResponse<ApiResult<int>>(request);
            return GetInnerResponse(response);
        }
        /// <summary>
        /// 装托
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<List<ApiCodeResponse>?> SavePalletAsync(SavePalletRequest request)
        {
            var client = GetClient<SavePalletRequest>();
            var response = await client.GetResponse<ApiResult<List<ApiCodeResponse>>>(request);
            return GetInnerResponse(response);
        }
        /// <summary>
        /// 拆箱
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<List<ApiCodeResponse>?> UnpackBoxesAsync(UnpackBoxesRequest request) 
        {
            var client = GetClient<UnpackBoxesRequest>();
            var response = await client.GetResponse<ApiResult<List<ApiCodeResponse>>>(request);
            return GetInnerResponse(response);
        }
        /// <summary>
        /// 拆托
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<object?> UnpackPalletAsync(UnpackPalletRequest request) 
        {
            var client = GetClient<UnpackPalletRequest>();
            var response = await client.GetResponse<ApiResult<object>>(request);
            return GetInnerResponse(response);
        }
        /// <summary>
        /// 刷新箱码数据
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Dictionary<string, BoxCodeCountResponse>?> RefreshBoxCodesAsync(RefreshBoxCodesRequest request)
        {
            var client = GetClient<RefreshBoxCodesRequest>();
            var response = await client.GetResponse<ApiResult<Dictionary<string, BoxCodeCountResponse>>>(request);
            return GetInnerResponse(response);
        }
        private ClientService<T> GetClient<T>()where T : RequestBase
        {
            var result = new ClientService<T>(_packingConfig, _userContext, _mapper, Logger);
            if (_userContext.Token != null)
            {
                result.SetToken(_userContext.Token);
            }
            return result;
        }
        private static T? GetInnerResponse<T>(ApiResult<T>? response)
        {
            if (response != null)
            {
                if (response.Code == GlobalErrorCode.Ok )
                {
                    return response.Data;
                }
                else
                {
                    throw new ApiException(response.Code, response.ErroMessage);
                }
            }
            throw new ApiException(GlobalErrorCode.NoResponse);
        }
    }
}
